package com.ruoyi.house.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.house.service.IZuInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.house.mapper.MyFavoriteMapper;
import com.ruoyi.house.domain.MyFavorite;
import com.ruoyi.house.service.IMyFavoriteService;

/**
 * 我的收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@Service
public class MyFavoriteServiceImpl extends ServiceImpl<MyFavoriteMapper, MyFavorite> implements IMyFavoriteService {
    @Autowired
    private MyFavoriteMapper myFavoriteMapper;

    @Autowired
    private IZuInfoService zuInfoService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询我的收藏
     *
     * @param id 我的收藏主键
     * @return 我的收藏
     */
    @Override
    public MyFavorite selectMyFavoriteById(Long id) {
        return myFavoriteMapper.selectMyFavoriteById(id);
    }

    /**
     * 查询我的收藏列表
     *
     * @param myFavorite 我的收藏
     * @return 我的收藏
     */
    @Override
    public List<MyFavorite> selectMyFavoriteList(MyFavorite myFavorite) {
        List<MyFavorite> myFavorites = myFavoriteMapper.selectMyFavoriteList(myFavorite);
        myFavorites = myFavorites.stream().peek(fa-> {
            fa.setZuInfo(zuInfoService.selectZuInfoById(fa.getHouseZuInfoId()));
            fa.setUser(sysUserService.selectUserById(fa.getUserId()));
        }).collect(Collectors.toList());
        return myFavorites;
    }

    /**
     * 新增我的收藏
     *
     * @param myFavorite 我的收藏
     * @return 结果
     */
    @Override
    public int insertMyFavorite(MyFavorite myFavorite) {
        myFavorite.setCreateTime(DateUtils.getNowDate());
        return myFavoriteMapper.insertMyFavorite(myFavorite);
    }

    /**
     * 修改我的收藏
     *
     * @param myFavorite 我的收藏
     * @return 结果
     */
    @Override
    public int updateMyFavorite(MyFavorite myFavorite) {
        myFavorite.setUpdateTime(DateUtils.getNowDate());
        return myFavoriteMapper.updateMyFavorite(myFavorite);
    }

    /**
     * 批量删除我的收藏
     *
     * @param ids 需要删除的我的收藏主键
     * @return 结果
     */
    @Override
    public int deleteMyFavoriteByIds(Long[] ids) {
        return myFavoriteMapper.deleteMyFavoriteByIds(ids);
    }

    /**
     * 删除我的收藏信息
     *
     * @param id 我的收藏主键
     * @return 结果
     */
    @Override
    public int deleteMyFavoriteById(Long id) {
        return myFavoriteMapper.deleteMyFavoriteById(id);
    }

    @Override
    public int deleteMyFavoriteByUserIdAndHouseZuInfoId(Long userId, Long houseZuInfoId) {

        QueryWrapper<MyFavorite> myFavoriteQueryWrapper = new QueryWrapper<>();
        myFavoriteQueryWrapper.eq("user_id", userId).eq("house_zu_info_id", houseZuInfoId);

        return this.remove(myFavoriteQueryWrapper) ? 1: 0;
    }
}
